package CP0417;

/**
 * AddressBook
 *
 * Version 1.0
 * April 17, 2011
 *
 * Copyright 2011 by Gerry Goeke
 */

import CP0417.Person;

import java.util.*;

public class AddressBook {

    private LinkedList<Person> listBook;

//    public LinkedList<Person> ListBook() {
//        return listBook;
//    }

    public AddressBook() {
        listBook = new LinkedList<Person>();
    }

    /**
     * AddEntry
     *
     * Version 1.0
     * April 17, 2011
     *
     * Copyright 2011 by Gerry Goeke
     */
    public Person AddEntry(String firstName, String lastName,
                           String addressLine1, String addressLine2,
                           String city, String state, String zip,
                           String  phoneNumber) {

        Person newEntry = new Person();
        listBook.add(newEntry);

        newEntry.firstName = firstName;
        newEntry.lastName = lastName;
        newEntry.addressLine1 = addressLine1;
        newEntry.addressLine2 = addressLine2;
        newEntry.city = city;
        newEntry.state = state;
        newEntry.zip = zip;
        newEntry.phoneNumber = phoneNumber;
        return newEntry;
    }

    /**
     * EditEntry
     *
     * Version 1.0
     * April 17, 2011
     *
     * Copyright 2011 by Gerry Goeke
     */
    public Person EditEntry(Person originalPerson, Person editedPerson) {
        /*
         * Replace person with edited person.  First find original person in
         * book;  if found, replace values with new ones
         * if not found return null entry
         */
        Person returnedPerson = new Person();
        ListIterator<Person> itrBook = listBook.listIterator();
        while (itrBook.hasNext()) {
            if (originalPerson.equals(itrBook.next())) {
                returnedPerson = editedPerson;
                itrBook.set(editedPerson);
            }
        }
        return returnedPerson;
    }

    /**
     * DeleteEntry
     *
     * Version 1.0
     * April 17, 2011
     *
     * Copyright 2011 by Gerry Goeke
     */
    public Person DeleteEntry(Person personToDelete) {
        Person returnedPerson = new Person();
        ListIterator<Person> itrBook = listBook.listIterator();
        while (itrBook.hasNext()) {
            if (personToDelete.equals(itrBook.next())) {
                returnedPerson = personToDelete;
                itrBook.remove();
            }
        }
        return returnedPerson;
    }

    /**
     * SortEntriesByName
     *
     * Version 1.0
     * April 26, 2011
     *
     * Copyright 2011 by Gerry Goeke
     */
    public LinkedList<Person> SortEntriesByName() {

        /*
         * Implement NameComparator class with compare method
         * so that can use Collections sort capability
         */
        class NameComparator implements Comparator<Person>
        {
            public int compare (Person p1, Person p2)
            {
                return p1.lastName.compareTo(p2.lastName);
            }
        }

        Collections.sort(listBook,new NameComparator());
        return listBook;
    }


    /**
     * FindText
     *
     * Version 1.0
     * April 26, 2011
     *
     * Copyright 2011 by Gerry Goeke
     */
    public Person FindText(String textToFind) {
        ListIterator<Person> itrBook = listBook.listIterator();
        Person curPerson = null;
        boolean notFound = true;
        while (notFound) {
            curPerson = itrBook.next();
            /*
             * Concatenate all items of collection together & see if it
             * contains the find string
             */
            if ((curPerson.lastName + curPerson.firstName
                    + curPerson.addressLine1+ curPerson.addressLine2
                    + curPerson.city + curPerson.state + curPerson.zip
                    + curPerson.phoneNumber).contains(textToFind)) {
                notFound = false;
            }
        }

        if (notFound) {
            return null;
        }
        else {
            return curPerson;
        }
    }
}
